package com.bafenyi.zh.bafenyilib.config;

import android.app.Application;
import android.os.Handler;
import android.util.Log;

import com.bafenyi.zh.bafenyilib.BFYMethod;

import com.bafenyi.zh.bafenyilib.BFYMethodListener;
import com.bafenyi.zh.bafenyilib.BuildConfig;
import com.bafenyi.zh.bafenyilib.base.BFYBaseActivityLifecycleClassbacks;
import com.bafenyi.zh.bafenyilib.request.BFYRequest;
import com.bafenyi.zh.bafenyilib.request.BFYRequestListener;
import com.bafenyi.zh.bafenyilib.util.PreferenceUtil;
import com.blankj.utilcode.util.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BFYConfig {
    /**
     * 初始化广告回调
     **/
    public static BFYMethodListener.AdCallBackForInit adCallBackForInit;
    /**
     * 开屏广告回调
     **/
    public static BFYMethodListener.AdCallBackForSplash adCallBackForSplash;

    /**
     * 应用名
     **/
    private static String appName;
    /**
     * 包名
     **/
    private static String pkgName;
    /**
     * 大航海统计secret
     **/
    private static String statisticalAppSecret;
    /**
     * 大航海统计appid
     **/
    private static String staticticalAppid;
    /**
     * 版本号
     **/
    private static String appVersion;
    /**
     * code号
     **/
    private static String appVersionCode;
    /**
     * more app pic
     **/
    private static ArrayList<String> moreAppPics;
    /**
     * application
     **/
    private static Application app;
    /**
     * app前后台监听
     **/
    private static BFYBaseActivityLifecycleClassbacks myActivityLifecycle;

    /**
     * SDK初始化（推广渠道专用）
     *
     * @param appName              应用名
     * @param pkgName              包名
     * @param staticticalAppid     大航海统计appid
     * @param statisticalAppSecret 大航海统计secret
     * @param appVersion           版本号
     * @param appVersionCode       code号
     * @param flavor               渠道
     * @param oaid                 oaid上报
     * @param app                  application
     */
    public static void init(String appName,
                            String pkgName,
                            String staticticalAppid,
                            String statisticalAppSecret,
                            String appVersion,
                            String appVersionCode,
                            String flavor,
                            String oaid,
                            Application app) {
        setApp(app);
        setOaid(oaid);
        init(appName , pkgName , staticticalAppid , statisticalAppSecret , appVersion , appVersionCode , flavor , app);
    }

    /**
     * SDK初始化
     *
     * @param appName              应用名
     * @param pkgName              包名
     * @param staticticalAppid     大航海统计appid
     * @param statisticalAppSecret 大航海统计secret
     * @param appVersion           版本号
     * @param appVersionCode       code号
     * @param flavor               渠道
     * @param app                  application
     */
    public static void init(String appName,
                            String pkgName,
                            String staticticalAppid,
                            String statisticalAppSecret,
                            String appVersion,
                            String appVersionCode,
                            String flavor,
                            Application app) {
        setApp(app);
        setIsAppMarket(flavor);
        setAppName(appName);
        setPkgName(pkgName);
        setStatisticalAppSecret(statisticalAppSecret);
        setStaticticalAppid(staticticalAppid);
        setAppVersion(appVersion);
        setAppVersionCode(appVersionCode);
        myActivityLifecycle = new BFYBaseActivityLifecycleClassbacks();
        app.registerActivityLifecycleCallbacks(myActivityLifecycle);
    }

    private static void setIsAppMarket(String flavor) {
        PreferenceUtil.put("isAppMarket", !flavor.contains("juliang") && !flavor.contains("kuaishou"));
    }
    public static boolean getIsAppMarket() {
        return PreferenceUtil.getBoolean("isAppMarket", true);
    }


    private static void setOaid(String oaid) {
        PreferenceUtil.put("oaid", oaid);
    }
    public static String getOaid() {
        return PreferenceUtil.getString("oaid", "");
    }

    private static void setAppName(String appName) {
        PreferenceUtil.put("appName", appName);
    }

    public static String getAppName() {
        return PreferenceUtil.getString("appName", "");
    }

    private static void setPkgName(String pkgName) {
        PreferenceUtil.put("pkgName", pkgName);
    }

    public static String getPkgName() {
        return PreferenceUtil.getString("pkgName", "");
    }

    private static void setStatisticalAppSecret(String statisticalAppSecret) {
        PreferenceUtil.put("statisticalAppSecret", statisticalAppSecret);
    }

    public static String getStatisticalAppSecret() {
        return PreferenceUtil.getString("statisticalAppSecret", "");
    }

    private static void setStaticticalAppid(String staticticalAppid) {
        PreferenceUtil.put("staticticalAppid", staticticalAppid);
    }

    public static String getStaticticalAppid() {
        return PreferenceUtil.getString("staticticalAppid", "");
    }

    private static void setAppVersion(String appVersion) {
        PreferenceUtil.put("appVersion", appVersion);
    }

    public static String getAppVersion() {
        return PreferenceUtil.getString("appVersion", "");
    }

    private static void setAppVersionCode(String appVersionCode) {
        PreferenceUtil.put("appVersionCode", appVersionCode);
    }

    public static String getAppVersionCode() {
        return PreferenceUtil.getString("appVersionCode", "");
    }

    public static void setApp(Application app) {
        BFYConfig.app = app;
    }

    public static Application getApp() {
        return app;
    }


    /**
     * 跳转web更新apk
     **/
    public static String getUpdateApk() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("updateApk")) {
                return "";
            }
            return jsonObject.getString("updateApk");
        } catch (JSONException e) {
            return "";
        }
    }

    /**
     * 展示腾讯广告
     **/
    public static String getAdServer() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("adServer")) {
                return "";
            }
            return jsonObject.getString("adServer");
        } catch (JSONException e) {
            return "";
        }
    }

    /**
     * 是否展示更多应用
     **/
    public static String getNewMoreApp() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "off";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("new_more_app")) {
                return "off";
            }
            return jsonObject.getString("new_more_app");
        } catch (JSONException e) {
            return "off";
        }
    }

    /**
     * 线上版本号
     **/
    public static String getUpdateVersion() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "0";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("update_version")) {
                return "0";
            }
            Pattern p = Pattern.compile("[^0-9]");
            Matcher m = p.matcher(jsonObject.getString("update_version"));
            return m.replaceAll("");
        } catch (JSONException e) {
            return "0";
        }
    }

    /**
     * 是否展示强制更新
     **/
    public static String getNewForceUpdate() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "off";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("new_forceUpdate")) {
                return "off";
            }
            return jsonObject.getString("new_forceUpdate");
        } catch (JSONException e) {
            return "off";
        }
    }

    /**
     * 更多应用url
     **/
    public static String getMarketUrl() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "https://m.8fenyi.cn/index.html?language=cn";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("marketUrl")) {
                return "https://m.8fenyi.cn/index.html?language=cn";
            }
            return jsonObject.getString("marketUrl");
        } catch (JSONException e) {
            return "https://m.8fenyi.cn/index.html?language=cn";
        }
    }

    /**
     * 最后修改时间
     **/
    public static String getLastUpdateTime() {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return "";
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull("lastUpdateTime")) {
                return "";
            }
            return jsonObject.getString("lastUpdateTime");
        } catch (JSONException e) {
            return "";
        }
    }

    /**
     * 获取其他配置参数
     **/
    public static String getOtherParamsForKey(String key, String defaultValue) {
        String params = PreferenceUtil.getString("params", "");
        if (params.isEmpty()) {
            return defaultValue;
        }
        try {
            JSONObject jsonObject = new JSONObject(params);
            if (jsonObject.isNull(key)) {
                return defaultValue;
            }
            return jsonObject.getString(key);
        } catch (JSONException e) {
            return defaultValue;
        }
    }


    /**
     * 轮播图pics
     **/
    public static ArrayList<String> getMoreAppPics() {
        return moreAppPics;
    }

    public static void setMoreAppPics(ArrayList<String> moreAppPics) {
        BFYConfig.moreAppPics = moreAppPics;
    }
}
